package cmdClient

import (
	"encoding/base64"
	"fmt"
	"net"
	"remote-go/client/data"
	"remote-go/client/util"
	"strings"
)

func ExeCommand(command string, conn net.Conn) {
	cmdArray := strings.Split(command, " ")
	cmdSlice := cmdArray[1:]
	//cmd命令实时接收回调函数
	call := func(out []byte, outErr error) {
		if outErr != nil {
			out = []byte(outErr.Error())
		}
		out = append(out, byte('\n'))
		// 解决命令行输出编码问题
		if data.Charset != "utf-8" {
			out = []byte(util.ConvertStrCharset(string(out), data.Charset, "utf-8"))
		}
		fmt.Print(string(out))
		encoded := base64.StdEncoding.EncodeToString(out)
		conn.Write([]byte(encoded + "\n"))
	}
	util.CommandTimeOut(call, cmdArray[0], cmdSlice...)
}
